/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * ProjectViewContentProvider.java
 * 
 * Content provider for the project explorer (Tree view )
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio.ui.views;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import edu.cmu.isr.dinvenio.Activator;
import edu.cmu.isr.dinvenio.model.DepsGraph;
import edu.cmu.isr.dinvenio.model.DepsGraphNodeSet;

public class ProjectViewContentProvider implements ITreeContentProvider{

	@Override
	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof DepsGraph) {
			Object[] objs = {"Graph"};
			return objs;
		}
		else if(parentElement instanceof String){
			if (parentElement.equals("Graph")){
				Object[] objs = {"Node Set","Layers"};
				return objs;
			}
			else if(parentElement.equals("Node Set")){
				return Activator.myController.getGraphModel().getAllNodeSets().values().toArray();
			}
			else if(parentElement.equals("Layers")){
				return Activator.myController.getGraphModel().getAllLayers().values().toArray();
			}
			else{
				return null;
			}
		}
		else if(parentElement instanceof DepsGraphNodeSet){
			return ((DepsGraphNodeSet)parentElement).nodes.values().toArray();
		}
		return null;
	}

	@Override
	public Object getParent(Object element) {
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		if(element instanceof DepsGraph) {
			return !((DepsGraph)element).getAllNodeSets().isEmpty();
		}
		else if(element instanceof DepsGraphNodeSet){
			return !((DepsGraphNodeSet)element).nodes.isEmpty();
		}
		else if(element instanceof String){
			return true;
		}
		return false;
	}

	@Override
	public Object[] getElements(Object iE) {
		if(iE instanceof DepsGraph) {
			Object[] objs = {"Graph"};
			return objs;
		}
		else if(iE instanceof String){
			if (iE.equals("Graph")){
				Object[] objs = {"Node Set","Layers"};
				return objs;
			}
			else if(iE.equals("Node Set")){
				return Activator.myController.getGraphModel().getAllNodeSets().values().toArray();
			}
			else if(iE.equals("Layers")){
				return Activator.myController.getGraphModel().getAllLayers().values().toArray();
			}
			else{
				return null;
			}
		}
		else if(iE instanceof DepsGraphNodeSet){
			return ((DepsGraphNodeSet)iE).nodes.values().toArray();
		}
		else if(iE instanceof String){
			return Activator.myController.getGraphModel().getAllNodeSets().values().toArray();
		}
		return null;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub
		
	}

}
